<?php

namespace app\common\command;

use app\common\service\CostCheck as CostCheckService;
use mes\Mes;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;

/**
 * 成本管理》核价与补损跟进，任务自动生成
 */
class CostCheck extends Command
{

    protected $logName = 'CostCheck';
    protected $mes = '';
    protected $isPrint = 1;    // 是否打印信息到终端
    /**
     * @var \app\common\service\CostCheck
     */
    protected $costCheckService;


    protected function configure()
    {
        $this->setName('CostCheck')
            ->addOption('type', 't', Option::VALUE_OPTIONAL, '分类', '')
            ->setDescription('成本管理》核价与补损跟进，任务自动生成');
        $this->costCheckService = new CostCheckService();
    }

    protected function execute(Input $input, Output $output)
    {
        set_time_limit(0);
        ini_set('memory_limit', '1000M');
        error_reporting(E_ALL & ~E_NOTICE);
        $type = $input->getOption('type');
        define('ADMIN_ID', 0);
        switch ($type) {
            case 'sync_data':
                $res = $this->costCheckService->sync_data();
                echo "Done,code:{$res['code']} msg:{$res['msg']}" . PHP_EOL;
                return false;
                break;
            case 'one_wan':
                $config_0 = db('cost_check_config')->whereRaw("type=0")->order('id desc')->find();
                isset($config_0['id']) && $config_0 = json_decode($config_0['content'], true);
                $config_10000 = db('cost_check_config')->whereRaw("type=10000")->order('id desc')->find();
                isset($config_10000['id']) && $config_10000 = json_decode($config_10000['content'], true);
                $res = $this->costCheckService->set_cost_check_select_10000_all($config_10000);
                echo "Done,code:{$res['code']} msg:{$res['msg']}" . PHP_EOL;
                return false;
                break;
            case 'c_1':
                $config_0 = db('cost_check_config')->whereRaw("type=0")->order('id desc')->find();
                isset($config_0['id']) && $config_0 = json_decode($config_0['content'], true);
                $config_10000 = db('cost_check_config')->whereRaw("type=10000")->order('id desc')->find();
                isset($config_10000['id']) && $config_10000 = json_decode($config_10000['content'], true);
                $res = $this->costCheckService->set_cost_check_select_1($config_0);
                echo "Done,code:{$res['code']} msg:{$res['msg']}" . PHP_EOL;
                return false;
                break;
            case 'c_2':
                $config_0 = db('cost_check_config')->whereRaw("type=0")->order('id desc')->find();
                isset($config_0['id']) && $config_0 = json_decode($config_0['content'], true);
                $config_10000 = db('cost_check_config')->whereRaw("type=10000")->order('id desc')->find();
                isset($config_10000['id']) && $config_10000 = json_decode($config_10000['content'], true);
                $res = $this->costCheckService->set_cost_check_select_2($config_0);
                echo "Done,code:{$res['code']} msg:{$res['msg']}" . PHP_EOL;
                return false;
                break;
            case 'c_3':
                $config_0 = db('cost_check_config')->whereRaw("type=0")->order('id desc')->find();
                isset($config_0['id']) && $config_0 = json_decode($config_0['content'], true);
                $config_10000 = db('cost_check_config')->whereRaw("type=10000")->order('id desc')->find();
                isset($config_10000['id']) && $config_10000 = json_decode($config_10000['content'], true);
                $res = $this->costCheckService->set_cost_check_select_3($config_0);
                echo "Done,code:{$res['code']} msg:{$res['msg']}" . PHP_EOL;
                return false;
                break;
            default:
//                echo "Done" . PHP_EOL;
                return $this->init();
                break;
        }
    }

    private function init()
    {
        define('ADMIN_ID', 0);
        $res = $this->costCheckService->set_cost_check_all();
        echo "Done,code:{$res['code']} msg:{$res['msg']}" . PHP_EOL;
        return false;
    }
}